<?php
/**
 * Language
 *
 * 언어팩 관리
 *
 * @package boot
 * @author  Platform Team
 * @deprecated
 */

class Language
{
    const GLOBAL_FILE_NAME = '_global';

    const TYPE_PHP = 'php';
    const TYPE_JS = 'js';

    /**
     * 인스턴스
     * @var Language
     */
    private static $instance;

    /**
     * 옵티마이져 객체
     * @var Optimizer
     */
    private static $Optmizer;

    /**
     * 언어코드
     */
    private static $sCode;

    /**
     * 언어팩 배열
     * @var array(
     *  '리소스루트' => array(
     *      '키' => '값'
     *  ),...
     * )
     */
    private static $aLang;

    /**
     * JS 언어팩 사용여부
     */
    private static $isJsUse = false;

    /**
     * 동작 여부
     */
    private static $isProgress = false;

    /**
     * 생성자
     */
    private function __construct() {}

    /**
     * singleton
     *
     * @return Language
     */
    public static function getInstance()
    {
        if (!self::$instance) self::$instance = new self;

        return self::$instance;
    }

    /**
     * 인스턴스 반환
     *
     * @return Language
     */
    public static function instance()
    {
        return self::$instance;
    }

    /**
     * 인스턴스 파괴
     */
    public static function instancedel()
    {
        self::$Optmizer = null;
        self::$sCode = null;
        self::$aLang = null;
        self::$isJsUse = false;
        self::$isProgress = false;

        self::$instance = null;
    }

    /**
     * 초기화
     *
     * @param $Optmizer (옵티마이져 객체)
     * @param string $sCode    (언어 코드)
     * @param boolean $isJs     (JS 언어팩 사용 여부  - 기본 false)
     * @return boolean
     */
    public function initialize($Optimizer, $sCode, $isJs=false)
    {
        self::$Optmizer     = $Optimizer;
        self::$sCode        = $sCode;
        self::$aLang        = array();
        self::$isJsUse      = ($isJs === true) ? true : false;
        self::$isProgress   = true;

        self::_loadJsCore();

        return true;
    }

    /**
     * 읽어오기
     *
     * @param string $sRoot (리소스 루트 디렉토리)
     * @param string $sName (이름)
     * @return mixed
     */
    public function read($sRoot, $sName)
    {
        return self::$aLang[$sRoot][$sName];
    }

    /**
     * 언어팩 할당
     *
     * @param string $sRoot    (리소스 루트 디렉토리)
     * @param string $sPath    (경로)
     * @return boolean
     */
    public function import($sRoot, $sPath)
    {
        if (self::$isProgress === false) {
            throw new Exception('Language initialize failed', E_ERROR);
        }

        $sKey = $sRoot;

        $aSplit = explode('/', $sPath);

        $sPhpRes = self::_getResourceSpace(self::TYPE_PHP);
        $sJsRes = self::_getResourceSpace(self::TYPE_JS);

        $sPhpRootPath = $sRoot . '/' . $sPhpRes;
        $sJsRootPath = $sRoot . '/' . $sJsRes;

        $sPhpPath = $sPhpRootPath . '/' . $sPath . EXT;

        if (is_array($aSplit)) {
            $s = '';
            foreach ($aSplit as $split) {
                $sGlobalPath = $s . '/' . self::GLOBAL_FILE_NAME;

                $sPhpGlobalPath = $sPhpRootPath . $sGlobalPath . EXT;

                self::_importPhp($sKey, $sPhpGlobalPath);
                self::_importJs($sJsRootPath, $sGlobalPath);

                $s .= '/' . $split;
            }
        }


        $r = self::_importPhp($sKey, $sPhpPath);
        $r1 = self::_importJs($sJsRootPath, $sPath);

        return ($r && $r1);
    }

    /**
     * 언어팩 PHP include
     *
     * @param string $sKey
     * @param string $sFullPath
     * @return boolean
     */
    private static function _importPhp($sKey, $sFullPath)
    {
        if (!is_file($sFullPath)) {
            return false;
        }

        include($sFullPath);

        self::$aLang[$sKey] = array_merge((array) self::$aLang[$sKey], (array) $lang);

        return true;
    }

    /**
     * 언어팩 JS include
     *
     * @param string $sRootPath
     * @param string $sPath
     * @return boolean
     */
    private static function _importJs($sRootPath, $sPath)
    {
        if (self::$isJsUse === true) {
            return self::$Optmizer->addJS($sRootPath, $sPath);
        }

        return true;
    }

    /**
     * 언어팩 JS 코어 부분 include
     */
    private static function _loadJsCore()
    {
        if (self::$isJsUse === true) {
            self::$Optmizer->addJS(FW_DIR_FRAMEWORK_RESOURCE . '/' . 'js', 'language');
        }
    }

    /**
     * 각 타입의 리소스 루트 영역의 패쓰 문자 반환
     *
     * @param string $sType
     * @return string
     */
    private static function _getResourceSpace($sType)
    {
        return 'lang' . '/' . self::$sCode . '/' . $sType;
    }
}
